import pandas as pd
import numpy as np
from datetime import datetime

# 读取Excel文件并转化日期格式
df = pd.read_excel('ymData.xlsx')
df['date'] = pd.to_datetime(df['date'],format='%Y年%m月')

# 使用 strftime 将 Timestamp 转换为字符串类型
df['date'] = df['date'].dt.strftime('%y年%m月').tolist()
# 2. 将dataframe中的数据类型从str转换为datetime类型
from datetime import *
df['date'] = df['date'].map(lambda x: datetime.strptime(x, "%y年%m月"))
df['date']


# 3. 使用map()或apply()函数对日期特征进行年、月、日的提取

df["year"] = df["date"].map(lambda x: x.year)
df["month"] = df["date"].map(lambda x: x.month)
df

# 将特征和标签分开

X = df[['year', 'month']].values
y = df["date"].values



# 划分数据集
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

# 建立SVM模型
from sklearn.svm import SVC
classifier = SVC(kernel='linear', random_state=0)
classifier.fit(X_train, y_train)

# 预测测试集结果并评价模型
y_pred = classifier.predict(X_test)
from sklearn.metrics import confusion_matrix, classification_report
cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)
print(cm)
print(cr)